package com.hello.sandbox.utils;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class DrawableUtils {
  public static Bitmap drawableToBitmap(Drawable drawable, int width, int height) {
    if (drawable == null) return null;

    if (drawable instanceof BitmapDrawable) return ((BitmapDrawable) drawable).getBitmap();

    Bitmap.Config config =
        drawable.getOpacity() != PixelFormat.OPAQUE
            ? Bitmap.Config.ARGB_8888
            : Bitmap.Config.RGB_565;

    Bitmap bitmap = Bitmap.createBitmap(width, height, config);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, width, height);
    drawable.draw(canvas);
    return bitmap;
  }
}
